<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\api\model\ProjectLockPerformanceClass;
use app\api\model\ProjectLockPerOrg;
use app\api\model\ProjectLockReviewResult;
use app\api\model\ProjectPerformanceClass;
use app\api\model\ProjectPerformanceContent;
use app\api\model\ProjectPerformanceSpot;
use app\api\model\ProjectReviewResult;
use think\facade\Db;
use think\facade\Queue;
use app\api\model\ProjectInfo as ProjectInfoModel;
use app\api\model\ProjectPerOrg as ProjectPerOrgModel;
use app\api\model\ProjectReviewResult as ProjectReviewResultModel;
use app\api\model\ProjectPerOrgPeople as ProjectPerOrgPeopleModel;
use app\api\model\ProjectLockReviewResult as ProjectLockReviewResultModel;
use app\api\model\ProjectLockPerformanceClass as ProjectLockPerformanceClassModel;
use app\api\model\ProjectLockPerformanceContent as ProjectLockPerformanceContentModel;
use app\api\model\ProjectLockPerformanceSpot as ProjectLockPerformanceSpotModel;
use app\api\model\ProjectLockPerOrg as ProjectLockPerOrgModel;
use app\api\model\ProjectPerformance as ProjectPerformanceModel;
use app\api\model\ProjectLockReviewResult as ProjectReviewLockResultModel;
use app\api\model\ProjectLockPerOrgPeople as ProjectLockPerOrgPeopleModel;
use app\api\model\ProjectTeamScore as ProjectTeamScoreModel;
use app\api\model\ProjectTeaminfo as ProjectTeaminfoModel;
use app\api\model\ProjectPerOrg;
use app\Request;

class Tmp extends Base
{

    /**
     *自动修正项目成员分数
     */
    public function updateProjectTeamScore($month){
         try{
             //$month = "2021-03-01";
             $month = strtotime($month);
             //取出该成员的所有项目的分求和
             $ProjectTeamScoreModel = new ProjectTeamScoreModel;
             //查询团队成员
             $teamScoreInfo = array();
             $teamInfo = new ProjectTeaminfoModel;
             $teamArray = $teamInfo->distinct('uid,zn_name')->where('status','1')->select()->toArray();
             $sql = "";
             foreach ($teamArray as $v){
                 
                     $teamScoreInfo = $ProjectTeamScoreModel->alias('ts')
                         ->field('ts.month,ts.project_id,ts.id,SUM(ts.score) as score,rw.id as rw_id')
                         ->join('project_per_org_people op','ts.uid = op.uid')
                         ->join('project_teaminfo t','ts.uid = t.uid and ts.project_id = t.project_id')
                         ->join('project_review_result rw','op.id = rw.uid AND rw.spot_id = 206 AND  rw.`month` LIKE '.$month)
                         ->where('ts.month','=',$month)
                         ->where('t.status','=','1')
                         ->where('ts.uid','=',$v['uid'])
                         ->select()->toArray();
                     if(isset($teamScoreInfo[0]['score'])){
                         //判断上下线
                         if($teamScoreInfo[0]['score']>4){
                             $teamScoreInfo[0]['score'] = 4;
                         }
                         if($teamScoreInfo[0]['score']<-4){
                             $teamScoreInfo[0]['score'] = -4;
                         }
                         //更新分数
                         $ProjectReviewResultModel = new ProjectReviewResultModel();
                         $sd['score'] = $teamScoreInfo[0]['score'];
                         $r = $ProjectReviewResultModel->where('id','=',$teamScoreInfo[0]['rw_id'])->save($sd);
                         $sql = $sql."sql:".$ProjectTeamScoreModel->getLastSql()."||||";
                     }  
                 
             }
            
            $error = ["status"=>true,"msg"=>$sql];
            return json($error);
             
         } catch (\Exception $e) {
             $error = ["status"=>false,"msg"=>$e->getMessage(),"code"=>$e->getCode()];
             return $error;
         }
    }    
 

}
